import os
from pathlib import Path
import json

def check_project_files():
    """检查项目文件结构和状态"""
    print("=== 项目文件检查 ===")
    
    # 项目根目录
    project_root = Path(".")
    
    # 检查主要目录
    main_dirs = [
        "app",
        "app/controllers", 
        "app/models",
        "app/repositories",
        "app/routers",
        "app/services",
        "app/sql",
        "app/utils",
        "static",
        "static/uploads",
        "templates",
        "configure",
        "task",
        "entity"
    ]
    
    print("\n1. 检查主要目录:")
    for dir_path in main_dirs:
        if os.path.exists(dir_path):
            print(f"✅ {dir_path}")
        else:
            print(f"❌ {dir_path} (缺失)")
    
    # 检查关键文件
    key_files = [
        "main.py",
        "requirements.txt",
        "Dockerfile",
        "docker-compose.yml",
        "app/__init__.py",
        "app/controllers/__init__.py",
        "app/repositories/__init__.py",
        "app/services/__init__.py",
        "app/utils/__init__.py"
    ]
    
    print("\n2. 检查关键文件:")
    for file_path in key_files:
        if os.path.exists(file_path):
            print(f"✅ {file_path}")
        else:
            print(f"❌ {file_path} (缺失)")
    
    # 检查Python文件数量
    print("\n3. Python文件统计:")
    python_files = []
    for root, dirs, files in os.walk("."):
        # 跳过虚拟环境和缓存目录
        if any(skip in root for skip in ["__pycache__", "venv", "env", ".git", "fast_api_env"]):
            continue
        
        for file in files:
            if file.endswith(".py"):
                python_files.append(os.path.join(root, file))
    
    print(f"Python文件总数: {len(python_files)}")
    
    # 按目录统计
    dir_stats = {}
    for file_path in python_files:
        dir_name = os.path.dirname(file_path)
        if dir_name not in dir_stats:
            dir_stats[dir_name] = 0
        dir_stats[dir_name] += 1
    
    print("\n各目录Python文件数量:")
    for dir_name, count in sorted(dir_stats.items()):
        if dir_name.startswith("./app"):
            print(f"  {dir_name}: {count} 个文件")
    
    # 检查文件大小
    print("\n4. 大文件检查 (>1MB):")
    large_files = []
    for root, dirs, files in os.walk("."):
        if any(skip in root for skip in ["__pycache__", "venv", "env", ".git", "fast_api_env"]):
            continue
        
        for file in files:
            file_path = os.path.join(root, file)
            try:
                size = os.path.getsize(file_path)
                if size > 1024 * 1024:  # 1MB
                    large_files.append((file_path, size))
            except:
                pass
    
    if large_files:
        for file_path, size in sorted(large_files, key=lambda x: x[1], reverse=True):
            size_mb = size / (1024 * 1024)
            print(f"  {file_path}: {size_mb:.2f}MB")
    else:
        print("  没有发现大文件")
    
    # 检查空文件
    print("\n5. 空文件检查:")
    empty_files = []
    for root, dirs, files in os.walk("."):
        if any(skip in root for skip in ["__pycache__", "venv", "env", ".git", "fast_api_env"]):
            continue
        
        for file in files:
            if file.endswith(".py"):
                file_path = os.path.join(root, file)
                try:
                    if os.path.getsize(file_path) == 0:
                        empty_files.append(file_path)
                except:
                    pass
    
    if empty_files:
        for file_path in empty_files:
            print(f"  {file_path}")
    else:
        print("  没有发现空文件")
    
    # 检查导入错误
    print("\n6. 检查可能的导入问题:")
    import_issues = []
    for file_path in python_files:
        if "test" in file_path.lower() or "fix" in file_path.lower():
            continue
        
        try:
            with open(file_path, 'r', encoding='utf-8') as f:
                content = f.read()
                
                # 检查常见的导入问题
                if "from . import" in content and not content.strip().startswith("#"):
                    import_issues.append(f"{file_path}: 相对导入可能有问题")
                
                if "import *" in content:
                    import_issues.append(f"{file_path}: 使用了通配符导入")
                    
        except Exception as e:
            import_issues.append(f"{file_path}: 读取失败 - {e}")
    
    if import_issues:
        for issue in import_issues:
            print(f"  ⚠️  {issue}")
    else:
        print("  没有发现明显的导入问题")

if __name__ == "__main__":
    check_project_files() 